﻿using System;
using System.Data;
using System.Data.Common;

using ZhangBY.Config;
using ZhangBY.Common;
using ZhangBY.Entity;




namespace ZhangBY.Data.SqlServer
{
    public partial class DataProvider : IDataProvider
    {
        /// <summary>
        /// 输出用户列表
        /// </summary>
        /// <returns></returns>
        public IDataReader GetListUser()
        {
            return DbHelper.ExecuteReader(CommandType.StoredProcedure, "[dbo].[sp_listuser]");
        }

        /// <summary>
        /// 根据用户ID删除用户记录，成功返回非零。
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public int DelUserByUserid(string userid)
        {
            DbParameter[] getPar ={
                                     DbHelper.MakeInParam("@userid",(DbType)SqlDbType.NChar,60,userid),
                                     DbHelper.MakeOutParam("@outnum",(DbType)SqlDbType.Int,2),
                                 };
            DbHelper.ExecuteNonQuery(CommandType.StoredProcedure, "sp_deluserbyuserid", getPar);
            return Convert.ToInt32(getPar[1].Value);
        }

        /// <summary>
        /// 列出完整用户信息
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public IDataReader GetListFullUserInfo(string userid)
        {
            DbParameter[] getPar = { DbHelper.MakeInParam("@userid", (DbType)SqlDbType.NChar, 60, userid), };
            return DbHelper.ExecuteReader(CommandType.StoredProcedure, "sp_listfulluserinfo", getPar);
        }

        /// <summary>
        /// 根据USERID更新(user表)用户记录
        /// </summary>
        /// <param name="userid">用户ID</param>
        /// <param name="nickname">昵称</param>
        /// <param name="userpass">用户密码</param>
        /// <param name="userstatus">用户状态</param>
        /// <returns></returns>
        public int UpUserByUserId(string userid, string nickname, string userpass, short userstatus,string questioners,string answers)
        {
            DbParameter[] getPar ={
                                     DbHelper.MakeInParam("@userid",(DbType)SqlDbType.NChar,60,userid),
                                     DbHelper.MakeInParam("@nickname",(DbType)SqlDbType.NChar,20,nickname),
                                     DbHelper.MakeInParam("@userpass",(DbType)SqlDbType.NChar,20,userpass),
                                     DbHelper.MakeInParam("@userstatus",(DbType)SqlDbType.TinyInt,8,userstatus),
                                     DbHelper.MakeInParam("@questioners",(DbType)SqlDbType.NChar,40,questioners),
                                     DbHelper.MakeInParam("@answers",(DbType)SqlDbType.NChar,40,answers)
                                 };
            string commandText = string.Format("UPDATE [{0}user] SET [nickname]=@nickname,[userpass]=@userpass,[userstatus]=@userstatus,[questioners]=@questioners,[answers]=@answers,[modifydate]=GETDATE() WHERE [userid]=@userid", BaseConfigs.GetTablePrefix);
            return DbHelper.ExecuteNonQuery(CommandType.Text, commandText, getPar);
        }

        /// <summary>
        /// 更新USERINFO如果记录不存在则插入新记录
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="name"></param>
        /// <param name="nationalities"></param>
        /// <param name="qq"></param>
        /// <param name="phone"></param>
        /// <param name="fax"></param>
        /// <param name="area"></param>
        /// <param name="address"></param>
        /// <param name="email"></param>
        /// <param name="introduction"></param>
        /// <param name="sex"></param>
        /// <returns></returns>
        public int UpdateOrInsertUserInfo(string userid, string name,
                                          string nationalities,
                                          string qq, string phone,
                                          string fax, string area,
                                          string address, string email,
                                          string introduction, short sex)
        {
            DbParameter[] getPar={
                DbHelper.MakeInParam("@userid",(DbType)SqlDbType.NChar,60,userid),
                DbHelper.MakeInParam("@name",(DbType)SqlDbType.NChar,20,name),
                DbHelper.MakeInParam("@nationalities",(DbType)SqlDbType.NChar,20,nationalities),
                DbHelper.MakeInParam("@qq",(DbType)SqlDbType.NChar,16,qq),
                DbHelper.MakeInParam("@phone",(DbType)SqlDbType.NChar,16,phone),
                DbHelper.MakeInParam("@fax",(DbType)SqlDbType.NChar,16,fax),
                DbHelper.MakeInParam("@area",(DbType)SqlDbType.NChar,50,area),
                DbHelper.MakeInParam("@address",(DbType)SqlDbType.NChar,60,address),
                DbHelper.MakeInParam("@email",(DbType)SqlDbType.NChar,40,email),
                DbHelper.MakeInParam("@introduction",(DbType)SqlDbType.NChar,100,introduction),
                DbHelper.MakeInParam("@sex",(DbType)SqlDbType.TinyInt,8,sex),
            };
            return DbHelper.ExecuteNonQuery(CommandType.StoredProcedure,"sp_updateorinsertuserinfo",getPar);

        }

        /// <summary>
        /// 根据用户ID更新ADMIN记录，如果不存在则插入新记录
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="admingroup">管理员组</param>
        /// <param name="adminlevel">管理员级别</param>
        /// <returns></returns>
        public int UpdateOrInserAdmin(string userid, short admingroup, short adminlevel)
        {
            DbParameter[] getPar = { 
                                   DbHelper.MakeInParam("@userid",(DbType)SqlDbType.NChar,60,userid),
                                   DbHelper.MakeInParam("@admingroup",(DbType)SqlDbType.TinyInt,8,admingroup),
                                   DbHelper.MakeInParam("@adminlevel",(DbType)SqlDbType.TinyInt,8,adminlevel),
                                   };
            return DbHelper.ExecuteNonQuery(CommandType.StoredProcedure,"sp_updateorinsertadmin",getPar);
 
        }
    }
}
